var gc = {
    setBtnStatus:function () {
        var row = $('#grid_gc').datagrid('getSelected');
        if (row && row.id) {
            $("#btn_gc_mod").show();
            $("#btn_gc_del").show();
        } else {
            $("#btn_gc_mod").hide();
            $("#btn_gc_del").hide();
        }
    }
}

$(function () {
    $("#qyjg_mc").qyjgzcombobox();
    //表格初始加载
    $('#grid_gc').datagrid({
        url:$.webPath() + "/organ/gc/gcList",
        toolbar:"#toolbar_gc",
        fitColumns:true,
        singleSelect:true,
        pageNumber: 1,
        pagination:true,
        fit:true,
        rownumbers:true,
        striped:true,
        columns:[[
            {field:'mc', title:'工程名称', width:50},
            {field:'qyjg_qc', title:'企业机构', width:30},
            {field:'shdz', title:'送货地址', width:20},
            {field:'yf', title:'运费(元/每吨)', width:16,
                formatter:function(value){
                    var val = formatMoney(value);
                    return val;
                }
            },
          /*  {field:"sfjg" ,title:"是否竣工",width:10,
                formatter:function (value) {
                    if (value == '是')
                        return '是';
                    else
                        return '否';
                }
            },*/
            {field:'bz', title:'备注', width:50}
        ]],
        onLoadSuccess:function(){
            gc.setBtnStatus();
        },
        onClickRow:function(){
            gc.setBtnStatus();
        }/*,
        rowStyler:function(index,row){
            if(row.sfjg == '否') {
                return 'background-color:rgba(205,201,201,0.64);';
            }
        }*/
    });

    //查询内容的加载
    $("#qyjg_mc").zcombobox({
        onChange:function (newValue, oldValue) {
            if (newValue != oldValue) {
                $("#parent_qyjg").combotree("reload", $.webPath() + "/organ/qyjg/qyjg_tree?qyjgId=" + newValue);
            }
        }
    });
    //**********按钮触发事件**********
    //新增按钮
    $("#btn_gc_add").click(function () {
        openAddGcDialog('新增工程', 'add_gc');
    });
    //修改按钮
    $("#btn_gc_mod").click(function () {
        openModifyGcDialog('修改工程', 'update_gc');
    });
    //删除按钮
    $("#btn_gc_del").click(function () {
        gc_destroyItem();
    });
    //查询按钮
    $("#btn_gc_search").click(function () {
        gc_search()
    });

    //双击事件
    $("#grid_gc").datagrid({
        onDblClickCell:function (index, field, value) {
            if (field == "mc") {
                openModifyGcDialog('修改工程', 'update_gc');
            }
        }
    });
});

//新增工程
function openAddGcDialog(title, action) {
    var href = $.webPath() + "/organ/gc/gc_content";
    var actionUrl = $.webPath() + "/organ/gc/" + action;
    showGcDialog(title, href, actionUrl);
}

//修改工程
function openModifyGcDialog(title, action) {
    var record = $("#grid_gc").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要更新的记录！", "warning");
        return;
    }
    var href = $.webPath() + "/organ/gc/gc_content?gcId=" + record.id;
    var actionUrl = $.webPath() + "/organ/gc/" + action;
    showGcDialog(title, href, actionUrl);
}

//弹出新增或修改的窗口
function showGcDialog(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:750,
        height:450,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'保存',
                handler:function () {
                    //$.messager.progress();
                    $(win).mask();
                    $('#form_gc').form('submit', {
                        url:actionUrl,
                        onSubmit:function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                //$.messager.progress('close');
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success:function (data) {
                            //$.messager.progress('close');
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_gc').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//删除工程
function gc_destroyItem() {
    var row = $('#grid_gc').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否删除该工程？', function (r) {
            if (r) {
                var index = $('#grid_gc').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/organ/gc/delete_gc",
                    dataType: 'json',
                    type: 'POST',
                    data: {gcId:row.id},
                    beforeSend: function () {
                        $("#layout_gc").mask();
                    },
                    success:function(data){
                        $("#layout_gc").mask("hide");
                        if(data.statusCode == 200){
                            $('#grid_gc').datagrid('deleteRow', index);
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        } else{
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
//                $.post($.webPath() + '/organ/gc/delete_gc', {gcId:row.id}, function (data) {
//                    var obj = eval('(' + data + ')');
//                    if (obj.statusCode == '200') {
//                        $('#grid_gc').datagrid('deleteRow', index);
//                        $.messager.show({
//                            title:'提示',
//                            msg:obj.message,
//                            timeout:3000,
//                            showType:'slide'
//                        });
//                    } else {
//                        $.messager.alert("提示", obj.message, "error");
//                    }
//                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要删除的工程！", "warning");
    }
}

//查询工程
function gc_search() {
    $('#grid_gc').datagrid('load', {
        mc:$("#toolbar_gc input[name='mc']").val(),
        qyjgName:$('#qyjg_mc').attr("_text") == null ? null : $('#qyjg_mc').attr("_text")
    });
}




